<template>
  <el-dialog
    :title="`${drawerProps.title}突击申请`"
    v-model="drawerVisible"
    :draggable="true"
    :overflow="true"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    top="30px"
    width="600px"
    :destroy-on-close="true"
  >
    <el-form
      ref="ruleFormRef"
      style="overflow-y: auto"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      label-width="120px"
      label-position="top"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item prop="startTime" label="开始时间">
        <el-date-picker v-model="drawerProps.row.startTime" type="datetime"> </el-date-picker>
      </el-form-item>
      <el-form-item prop="endTime" label="结束时间">
        <el-date-picker v-model="drawerProps.row.endTime" @change="onEndTimeChange" type="datetime"> </el-date-picker>
      </el-form-item>
      <el-form-item prop="workTime" label="工作时间">
        <el-input-number v-model="drawerProps.row.workTime" :min="0.1" controls-position="right"></el-input-number>
      </el-form-item>
      <el-form-item prop="workNote" label="突击工作内容">
        <el-input type="textarea" :rows="5" v-model="drawerProps.row.workNote"></el-input>
      </el-form-item>
      <el-form-item prop="isHoliday" label="是否节假日">
        <el-switch v-model="holiday" disabled="true"> </el-switch>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="drawerVisible = false">取消</el-button>
        <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">{{
          drawerProps.title.indexOf("新增") !== -1 ? "开始突击" : "确定"
        }}</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { RushJob } from "@/api/types/base";
import { BusinessType, BusinessOptType } from "@/enums/sysEnum";
import { useUserAction } from "@/hooks/useUserAction";
import { calculateOvertimeHours } from "@/utils";

const { addUserAction } = useUserAction(BusinessType.RUSHJOB);

const rules = reactive({
  workNote: [{ required: true, message: "请输入突击内容" }]
});

interface DrawerProps {
  title: string;
  tbName: string;
  isView: boolean;
  row: Partial<RushJob>;
  api?: (tabName: string, params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  tbName: "",
  row: {}
});

const holiday = ref(false);

// 接收父组件传过来的环境
const acceptParams = async (params: DrawerProps) => {
  if (drawerProps.value.row.isHoliday === "1") {
    holiday.value = true;
  }
  drawerProps.value = params;
  drawerVisible.value = true;
};

const onEndTimeChange = () => {
  let workTime = calculateOvertimeHours(drawerProps.value.row.startTime, drawerProps.value.row.endTime);
  drawerProps.value.row.workTime = workTime;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(drawerProps.value.tbName, drawerProps.value.row);
      ElMessage.success({ message: `${drawerProps.value.title}成功！` });
      addUserAction(drawerProps.value.row._id, BusinessOptType.UPDATE, drawerProps.value.row.workNote);
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
